﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany.Extented_Controls
{
    public partial class TableLayoutPanelExtended : TableLayoutPanel
    {
        private bool mBusy;
        private bool mVScroll;

        public TableLayoutPanelExtended()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            //this.AutoScroll = false;
        }

        public TableLayoutPanelExtended(IContainer container)
        {
            container.Add(this); 
            InitializeComponent();
            this.DoubleBuffered = true;
            //this.AutoScroll = false;
        }

        protected override CreateParams CreateParams
        {
            get
            {
                System.Windows.Forms.CreateParams cp = base.CreateParams;
                cp.ExStyle = cp.ExStyle | 0x2000000;
                return cp;
            }
        }

        protected override void OnLayout(LayoutEventArgs levent)
        {
            base.OnLayout(levent);
            bool vscroll = this.GetScrollState(ScrollableControl.ScrollStateVScrollVisible);
            if (vscroll != mVScroll && !mBusy)
            {
                mVScroll = vscroll;
                mBusy = true;
                this.BeginInvoke(new MethodInvoker(AdjustPanel));
            }
        }

        private void AdjustPanel()
        {
            if (mVScroll) this.Width += SystemInformation.VerticalScrollBarWidth;
            else this.Width -= SystemInformation.VerticalScrollBarWidth;
            mBusy = false;
        }
    }
}
